﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNAControls
{
    public class MulitlineTextBox : TextBox
    {
        public void AppendText(string text)
        {
            string[] words = text.Split(' ');

            string txt = "";

            string result = "";

            foreach (string word in words)
            {
                if (Font.MeasureString(txt + word).X > Size.X)
                {
                    result += "\n" + txt;
                    txt = word;
                    while (Font.MeasureString(txt).X > Size.X)
                    {
                        string subtxt = txt;
                        while (Font.MeasureString(subtxt).X > Size.X)
                        {
                            subtxt = subtxt.Remove(subtxt.Length - 1);
                        }
                        result += "\n" + subtxt;
                        txt = txt.Substring(subtxt.Length);
                    }
                }
                else
                {
                    txt += " " + word;
                }
            }
            if (txt.Length > 0)
            {
                result += "\n" + txt.Replace("\n", "");
            }
            Text += result;
        }
    }
}
